package com.xm.dao.background;

import com.xm.entity.CommoditySpecification;
import com.xm.entity.CommoditySpecificationType;
import com.xm.exception.SqlErrorException;
import com.xm.util.BaseUtil;
import org.intellij.lang.annotations.Language;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 商品规格
 * @author 康文程
 * @date 2024-1-9 下午 06:49
 */
public class CommoditySpecificationDao extends BaseUtil {
    /**
     * 根据commodity_id查询商品规格
     * @param commodityId 商品ID
     * @return 商品规格列表
     */
    public List<CommoditySpecification> getCommoditySpecificationsByCommodityId(Integer commodityId) {
        ResultSet rs = null;
        List<CommoditySpecification> specifications = new ArrayList<>();
        @Language("sql")
        String sql = "SELECT cs.*, cst.specification_type_name " +
                "FROM commodity_specification cs " +
                "JOIN commodity_specification_type cst " +
                "ON cs.specification_type_id = cst.specification_type_id " +
                "WHERE cs.commodity_id = ?";
        try {
            // 查询规格信息
            rs = executeQuery(sql, commodityId);
            while (rs.next()) {
                CommoditySpecification specification = new CommoditySpecification();
                specification.setSpecificationId(rs.getInt("specification_id"));
                specification.setSpecificationName(rs.getString("specification_name"));
                specification.setSpecificationImage(rs.getString("specification_image"));
                specification.setSpecificationType(new CommoditySpecificationType(rs.getString("specification_type_name")));
                specification.setAddPrice(rs.getBigDecimal("add_price"));
                specification.setIsDefault(rs.getInt("is_default"));
                specification.setCommodityId(rs.getInt("commodity_id"));
                specifications.add(specification);
            }
        } catch (SQLException e) {
            throw new RuntimeException("SQL语句错误", e);
        } finally {
            close(rs);
            close(null);
        }
        return specifications;
    }
    /**
     * 新增商品规格
     *
     * @param specification 规格对象
     * @return 新增成功返回true，失败返回false
     * @throws SqlErrorException SQL执行异常
     */
    public boolean insertCommoditySpecification(CommoditySpecification specification) throws SqlErrorException {
        @Language("sql")
        String sql = "INSERT INTO commodity_specification " +
                "(specification_name, specification_type_id, add_price, is_default, commodity_id) " +
                "VALUES (?, ?, ?, ?, ?)";
        Object[] params = {
                specification.getSpecificationName(),
                specification.getSpecificationTypeId(),
                specification.getAddPrice(),
                specification.getIsDefault(),
                specification.getCommodityId()
        };
        int result = executeUpdate(sql, params);
        return result > 0;
    }
    /**
     * 根据 specification_type_id 和 commodity_id 删除 commodity_specification
     *
     * @param specificationTypeId 规格类型ID
     * @param commodityId         商品ID
     * @return 删除成功返回 true，失败返回 false
     */
    public boolean deleteCommoditySpecification(Integer specificationTypeId, Integer commodityId) throws SqlErrorException {
        @Language("SQL")
        String sql = "DELETE FROM commodity_specification WHERE specification_type_id = ? AND commodity_id = ?";
        Object[] params = {specificationTypeId, commodityId};
        int result = executeUpdate(sql, params);
        return result > 0;
    }
    /**
     * 根据 specification_id 和 commodity_id 删除 commodity_specification
     *
     * @param specificationId 规格ID
     * @param commodityId     商品ID
     * @return 删除成功返回 true，失败返回 false
     */
    public boolean deleteCommoditySpecificationByIds(int specificationId, int commodityId) throws SqlErrorException {
        @Language("SQL")
        String sql = "DELETE FROM commodity_specification WHERE specification_id = ? AND commodity_id = ?";
        Object[] params = {specificationId, commodityId};
        int result = executeUpdate(sql, params);
        return result > 0;
    }
}
